package com.cip.master.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Calendar;

import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.Academic;
import com.cip.master.model.ClassYear;
import com.cip.master.model.Section;
import com.cip.master.model.Examination;
import com.cip.master.model.Result;
import com.cip.master.service.AcademicService;
import com.cip.master.service.ClassYearService;
import com.cip.master.service.SectionService;
import com.cip.master.service.ExaminationService;
import com.cip.admin.service.UserManager;
import com.cip.master.service.ResultService;
import com.cip.master.service.SubjectService;
import com.cip.master.model.Subject;
import com.cip.webapp.action.BaseAction;

import java.sql.Date;
import java.text.SimpleDateFormat;

public class ResultAction extends BaseAction{
	
	private ResultService  resultService;
	private ExaminationService examService;
	private ClassYearService classYearService;
	private AcademicService  academicService;
	private SectionService sectionService;
	private List<Academic> academicList;
	private List<Examination> examList;
	private List<ClassYear> classYears;
	private List<Section> sectionList; 
	private Result result;
	private List<Result> results;
	private String academicYear;
	private String classYear;
	private String sectionName;
	private String examName;
	private String studentId;
	private Map<String,List<Result>> examResult=new HashMap<String, List<Result>>();
	
	public String showResults() throws Exception {
		setAcademicList(academicService.getAllAcademicList());
		setClassYears(classYearService.getAllClassYear());
		setExamList(examService.getAllExamList());
		setSectionList(sectionService.getAllSection());
		setResults(resultService.getResultsBySearch(getAcademicYear(), getClassYear(), getSectionName(),
				getExamName()));
		setAcademicYear(getAcademicYear());
		return SUCCESS;
	}
	
	public String showResultsByCandidate() throws Exception {
		setAcademicList(academicService.getAllAcademicList());
		setClassYears(classYearService.getAllClassYear());
		setExamList(examService.getAllExamList());
		setSectionList(sectionService.getAllSection());
		setResults(resultService.getResultsByCandidate("", getClassYear(), getSectionName(),
				getExamName(),getStudentId()));
		setAcademicYear(getAcademicYear());
		return SUCCESS;
	}
	
	
	public String showResultSearch() throws Exception {
		setAcademicList(academicService.getAllAcademicList());
		setClassYears(classYearService.getAllClassYear());
		setExamList(examService.getAllExamList());
		setSectionList(sectionService.getAllSection());
		return SUCCESS;
	}
	
	
	public String showUserResults() throws Exception {
		User user = SecurityUtil.getLoggedInUser();
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		// i didn't touch here. removed from query @daoimpl vinoth
		/*int year = Calendar.getInstance().get(Calendar.YEAR);
		String academicYear = (year-1) +"-"+year;
		Academic academic = academicService.getAcademicByYear(academicYear);
		*/Map<String,List<Result>> resultMap=new HashMap<String, List<Result>>();
		List<Examination> examinationList=examService.getAllExamListByClass(user.getClassYear());
		for (Examination examination : examinationList) {
			List<Result> results=resultService.getResultsByCandidate("", user.getClassYear(), user.getSection(),String.valueOf(examination.getId()),user.getId().toString());
			resultMap.put(examination.getExamName(), results);
		}
		setExamResult(resultMap);
		return SUCCESS;
	}
	
	public ResultService getResultService() {
		return resultService;
	}

	public void setResultService(ResultService resultService) {
		this.resultService = resultService;
	}
	
	public AcademicService getAcademicService() {
		return academicService;
	}

	public void setAcademicService(AcademicService academicService) {
		this.academicService = academicService;
	}
	
	public ClassYearService getClassYearService() {
		return classYearService;
	}

	public void setClassYearService(ClassYearService classYearService) {
		this.classYearService = classYearService;
	}
	
	public ExaminationService getExamService() {
		return examService;
	}

	public void setExamService(ExaminationService examService) {
		this.examService = examService;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public List<Result> getResults() {
		return results;
	}

	public void setResults(List<Result> results) {
		this.results = results;
	}	
	
	public List<Examination> getExamList() {
		return examList;
	}

	public void setExamList(List<Examination> examList) {
		this.examList = examList;
	}

	public List<ClassYear> getClassYears() {
		return classYears;
	}

	public void setClassYears(List<ClassYear> classYears) {
		this.classYears = classYears;
	}
	
	public List<Academic> getAcademicList() {
		return academicList;
	}

	public void setAcademicList(List<Academic> academicList) {
		this.academicList = academicList;
	}
	
	public List<Section> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<Section> sectionList) {
		this.sectionList = sectionList;
	}
	
	public void setSectionService(SectionService sectionService) {
		this.sectionService = sectionService;
	}

	public SectionService getSectionService() {
		return sectionService;
	}
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public String getAcademicYear() {
		return academicYear;
	}

	public Map<String, List<Result>> getExamResult() {
		return examResult;
	}

	public void setExamResult(Map<String, List<Result>> examResult) {
		this.examResult = examResult;
	}

	public String getStudentId() {
		return studentId;
	}

	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}

	public void setAcademicYear(String academicYear) {
		this.academicYear = academicYear;
	}

	public String getClassYear() {
		return classYear;
	}

	public void setClassYear(String classYear) {
		this.classYear = classYear;
	}

	public String getSectionName() {
		return sectionName;
	}

	public void setSectionName(String sectionName) {
		this.sectionName = sectionName;
	}

	public String getExamName() {
		return examName;
	}

	public void setExamName(String examName) {
		this.examName = examName;
	}	
	
}
